/**
 *    Copyright 2009-2021 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.demo.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DbUtil {
  private static String driver;
  private static String url;
  private static String username;
  private static String passowrd;

  static {
    driver = "com.mysql.cj.jdbc.Driver";
    url = "jdbc:mysql://192.168.146.11:3306/mybatis";
    username = "root";
    passowrd = "123456";
  }

  /**
   * 获取连接
   * @return
   */
  public static Connection open() {
    try {
      Class.forName(driver);
      return (Connection) DriverManager.getConnection(url, username, passowrd);
    } catch (Exception e) {
      System.out.println("连接数据库失败！");
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 关闭连接
   * @param conn
   */
  public static void close(Connection conn) {
    if(conn != null) {
      try {
        conn.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }
}
